package comp5511.project04;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class InvertedIndex {
	
	private String noiseWordsFile = "src/ds13s-asg4-noise-words.txt";
	
	private HashMap<String, Set<String>> indexMap = new HashMap<String, Set<String>>();
	
	private HashSet<String> noiseWords = new HashSet<String>();
	
	private HashMap<String, Paper> paperMap = new HashMap<String, Paper>();
	
	public InvertedIndex() {
		BufferedReader reader = null;
		try{
			reader = new BufferedReader(new FileReader(noiseWordsFile));
			String line;
			while (((line = reader.readLine()) != null)) {
				String[] words = line.split(",");
				for(String word : words){
					word = word.trim();

					if((!word.equals(""))){
						noiseWords.add(word);
					}
				}
			}
			System.out.println("total noise words:" + noiseWords.size());
		}
		catch(Exception e){
			System.out.println("fail when loading noise words!");
		}
		finally{
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void index(String inputFile) throws Exception{
		BufferedReader reader = null;
		try{
			reader = new BufferedReader(new FileReader(inputFile));
			String line;
			int number=0;
			while (((line = reader.readLine()) != null)) {
				number++;
				if(number==1){
					continue;
				}
				String[] strArray = line.split("\\|");
				String pid = strArray[1].trim();
				String title = strArray[2].trim();
				String[] authors = strArray[3].trim().split(";");
				String[] keywords = strArray[4].trim().split(",");
				String path = strArray[5].trim();
				Paper paper = new Paper(pid, title, authors, keywords, path);
				paperMap.put(pid, paper);
				for(String item: keywords){
					String[] keys =item.split(" ");
					for(String key: keys){
						key = key.toLowerCase().toString();
						if(noiseWords.contains(key)){
							continue;
						}
						Set<String> pids = indexMap.get(key);
						if(pids==null){
							Set<String> set = new HashSet<String>();
							set.add(pid);
							indexMap.put(key, set);
						}
						else{
							pids.add(pid);
						}
					}
				}
			}
			System.out.println("indexing finished");
		}
		catch(Exception e){
			System.out.println("fail when creating index");
			throw e;
		}
		finally{
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public List<Paper> search(String keywords){
		String[] keywordArray = keywords.split(" ");
		List<Set<String>> list = new ArrayList<Set<String>>();
		for(String key: keywordArray){
			Set<String> pids = indexMap.get(key.toLowerCase().trim());
			list.add(pids);
		}
		Set<String> finalPids;
		if(list.size()==1){
			finalPids = list.get(0);
		}
		else{
			finalPids = new HashSet<String>();
			Set<String> pids = list.get(0);
			if(pids!=null && pids.size()!=0){
				for(String pid: pids){
					boolean flag = true;
					for(int i=1;i<list.size();i++){
						if(list.get(i)==null || list.get(i).size()==0){
							flag = false;
							break;
						}
						else{
							if(!list.get(i).contains(pid)){
								flag = false;
								break;
							}
						}
					}
					if(flag){
						finalPids.add(pid);
					}
				}
			}
		}

		List<Paper> result = new ArrayList<Paper>();
		if(finalPids==null){
			return null;
		}
		for(String pid: finalPids){
			result.add(paperMap.get(pid));
		}
		return result;
	}

}
